var express = require('express');
const axios = require('axios');
const https = require('https')
var router = express.Router();

router.use(function timeLog(req, res, next) {
  console.log('Time: ', Date.now())
  next()
})

router.get('/', function (req, res, next) {
  res.json({
    name: "tom",
    age: 18,
    sex: true
  });
});

router.get('/test', async function (req, res, next) {
  const token = "123.03b33d9b18f77a6b90bdd59d3ef88a0e.YQHFuBbJk9WC_OENC8NQ7BOmCB-77p1PyiIJcT-.Ik7Aeg"
  const url = `https://pan.baidu.com/rest/2.0/xpan/nas?method=uinfo&access_token=${token}`

  try {
    const response = await axios.get(url);
    console.log('response', response);
    // res.json(JSON.stringify(response.data));
    res.json(response.data);
  } catch (error) {
    res.status(500).send('报错了');
    console.log('error', error);
  }
});

router.get('/test2', async function (req, res, next) {
  const token = "123.03b33d9b18f77a6b90bdd59d3ef88a0e.YQHFuBbJk9WC_OENC8NQ7BOmCB-77p1PyiIJcT-.Ik7Aeg"
  const url = `https://pan.baidu.com/rest/2.0/xpan/nas?method=uinfo&access_token=${token}`
  // https.get(
  //   url,
  //   res1 => {
  //     let dataList = [];
  //     res1.on('data', chunk => {
  //       dataList.push(chunk);
  //     });
  //     res1.on('end', () => {
  //       const dataStr = Buffer.concat(dataList).toString()
  //       // console.log('dataList', Buffer.concat(dataList).toString());
  //       res.json(JSON.parse(dataStr));
  //     });
  //   }
  // ).on('error', err => {
  //     console.log('Error: ', err.message);
  // });
  https.get(
    url,
    res1 => {
        res.json(1);
    },
    err => {
        res.json(2);
    }
)
});

module.exports = router;
